package ${package.infrastructure}.persistence;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ${package.application}.api.request.${entity}QueryRequest;
import ${package.application}.api.response.${entity}QueryResponse;
import ${package.application}.repository.I${entity}ReadModelRepo;
import ${package.domain}.entity.${entity};
import ${package.domain}.repository.I${entity}Repo;
import ${package.common}.base.Pagination;
import ${package.infrastructure}.persistence.assembler.${entity}POAssembler;
import ${package.infrastructure}.persistence.dao.I${entity}Mapper;
import ${package.infrastructure}.persistence.po.${entity}PO;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ${table.comment!}
 *
 * @author ${author}
 * @since ${date}
 */
@Service
@AllArgsConstructor
public class ${entity}RepoImpl extends ServiceImpl<I${entity}Mapper, ${entity}PO>
        implements I${entity}Repo, I${entity}ReadModelRepo {

    /**
     * ${table.comment!}持久化对象结构映射
     */
    private final ${entity}POAssembler ${entity?uncap_first}POAssembler;

    @Override
    public List<${entity}QueryResponse> query(Pagination pagination, ${entity}QueryRequest queryRequest) {
        // 构造查询条件
        LambdaQueryWrapper<${entity}PO> lambdaQueryWrapper = Wrappers.lambdaQuery();
    <#if enableLogicDelete??>
        // 含有逻辑删除，查询未删除数据
        lambdaQueryWrapper.eq(${entity}PO::getDelFlg, FlagConstants.FALSE);
    </#if>

    <#list table.fields as field>
        <#if field.keyFlag>
        <#-- 普通字段 -->

        <#elseif field.propertyName != logicDeletePropertyName!''>
        // 如果 ${field.comment} 不为空
        if (ObjectUtils.isNotEmpty(queryRequest.get${field.propertyName?cap_first}())) {
            lambdaQueryWrapper.eq(${entity}PO::get${field.propertyName?cap_first}, queryRequest.get${field.propertyName?cap_first}());
        }
        </#if>
    </#list>

        // 默认按照主键倒序排序
        lambdaQueryWrapper.orderByDesc(${entity}PO::getId);

        // 分页查询
        Page<${entity}PO> page = super.page(Page.of(pagination.getPageNumber(), pagination.getPageSize()), lambdaQueryWrapper);
        pagination.setTotal(page.getTotal());
        // ${table.comment!}持久化对象 转 ${table.comment!}查询响应对象
        return ${entity?uncap_first}POAssembler.toQueryResponse(page.getRecords());
    }

    @Override
    public ${entity} queryById(${idPropertyType} ${entity?uncap_first}Id) {
        // 根据主键查询${table.comment!}
        ${entity}PO ${entity?uncap_first}PO = super.getById(${entity?uncap_first}Id);
        // ${table.comment!}持久化对象 转 ${table.comment!}响应对象
        return ${entity?uncap_first}POAssembler.toEntity(${entity?uncap_first}PO);
    }

    @Override
    public void create(${entity} ${entity?uncap_first}) {
        // ${table.comment!}实体转${table.comment!}持久化对象
        ${entity}PO ${entity?uncap_first}PO = ${entity?uncap_first}POAssembler.assembler(${entity?uncap_first});
        <#if enableLogicDelete??>
        // 创建时默认未删除
        ${entity?uncap_first}PO.setDelFlg(FlagConstants.FALSE);
        </#if>

        // 创建${table.comment!}
        super.save(${entity?uncap_first}PO);
    }

    @Override
    public void deleteById(${idPropertyType} ${entity?uncap_first}Id) {
    <#if enableLogicDelete??>
        // 逻辑删除${table.comment!}
        ${entity}PO ${entity?uncap_first}PO = new ${entity}PO();
        ${entity?uncap_first}PO.setId(${entity?uncap_first}Id);
        ${entity?uncap_first}PO.set${logicDeletePropertyName?cap_first}(FlagConstants.TRUE);

        super.updateById(${entity?uncap_first}PO);
    <#else>
        // 物理删除${table.comment!}
        super.removeById(${entity?uncap_first}Id);
    </#if>
    }

    @Override
    public void updateById(${entity} ${entity?uncap_first}) {
        // ${table.comment!}实体转${table.comment!}持久化对象
        ${entity}PO ${entity?uncap_first}PO = ${entity?uncap_first}POAssembler.assembler(${entity?uncap_first});
        // 更新${table.comment!}
        super.updateById(${entity?uncap_first}PO);
    }
}
